package com.fishery.monitor.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fishery.monitor.dao.WarningDao;
import com.fishery.monitor.mapper.WarningMapper;
import com.fishery.monitor.pojo.Equipment;
import com.fishery.monitor.pojo.MeteorologicalData;
import com.fishery.monitor.pojo.Warning;
import com.fishery.monitor.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class WarningService extends ServiceImpl<WarningMapper, Warning> {

	@Autowired
	private WarningDao warningDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 根据设备id查询
	 */
	public List<Warning> findByEquipmentId(String equipmentId){
		Map whereMap = new HashMap();
		whereMap.put("equipmentId",equipmentId);
		QueryWrapper<Warning> wrapper = new QueryWrapper<>();
		wrapper.allEq(whereMap,false);
		return warningDao.findAll(wrapper);
	}
	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Warning> findAll() {
		return this.list(null);
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public IPage<Warning> findSearch(Map whereMap, int page, int size) {
//		Specification<Warning> specification = createSpecification(whereMap);
//		PageRequest pageRequest =  PageRequest.of(page-1, size);
//		return warningDao.findAll(specification, pageRequest);
		Page<Warning> Page = new Page<>(page,size);
		QueryWrapper<Warning> wrapper = new QueryWrapper<>();
		HashMap<String, String> searchMap = new HashMap();
		searchMap.putAll(whereMap);
		searchMap.remove("checkItemName");
		searchMap.remove("typeId");
		searchMap.remove("startTime");
		searchMap.remove("endTime");
		wrapper.allEq(searchMap, false);
		return warningDao.findAll(Page, wrapper);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Warning> findSearch(Map whereMap) {
		QueryWrapper<Warning> wrapper = new QueryWrapper<>();
		HashMap<String, String> searchMap = new HashMap();
		searchMap.putAll(whereMap);
		searchMap.remove("checkItemName");
		searchMap.remove("typeId");
		searchMap.remove("startTime");
		searchMap.remove("endTime");
		searchMap.remove("baseId");
		wrapper.allEq(searchMap, false);
		return warningDao.findAll(wrapper);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Warning findById(String id) {
		return this.findById(id);
	}

	/**
	 * 增加
	 * @param warning
	 */
	public void add(Warning warning) {
		warning.setId( idWorker.nextId()+"" );
//		Date date = new Date();
//		Timestamp timestamp = new Timestamp(date.getTime());
//		warning.setUpdateDate(timestamp);
		this.save(warning);
	}

	/**
	 * 修改
	 * @param warning
	 */
	public void update(Warning warning) {
		this.save(warning);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		this.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */

}
